package com.youme.section.shop;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import com.hjq.base.BaseDialog;
import com.hjq.base.action.AnimAction;
import com.hyphenate.easeim.R;
import com.youme.model.FoodAdd;
import com.youme.section.shop.adapter.RecyclerAdapter;

import java.util.List;

import androidx.annotation.StringRes;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public final class ShopCarDialog {

    public static final class Builder
        extends BaseDialog.Builder<Builder> {

        private OnListener mListener;
        private boolean mAutoDismiss = true;

        private final RecyclerAdapter mAdapter;
        private final AppCompatImageView mCancelView;

        public Builder(Context context, List<FoodAdd> foodAdds) {
            super(context);
            setContentView(R.layout.dialog_diancai);
            setAnimStyle(AnimAction.BOTTOM);
            RecyclerView recyclerView = findViewById(R.id.diancai_recycle);
            mCancelView = findViewById(R.id.iv_dinacai_closer);
            LinearLayoutManager manager = new LinearLayoutManager(context);
            recyclerView.setLayoutManager(manager);
            mAdapter = new RecyclerAdapter(foodAdds, getContext());
            recyclerView.setAdapter(mAdapter);
            setOnClickListener(R.id.iv_dinacai_closer);
        }

        @Override
        public Builder setGravity(int gravity) {
            switch (gravity) {
                // 如果这个是在中间显示的
                case Gravity.CENTER:
                case Gravity.CENTER_VERTICAL:
                    // 不显示取消按钮
                    setCancel(null);
                    // 重新设置动画
                    setAnimStyle(AnimAction.SCALE);
                    break;
                default:
                    break;
            }
            return super.setGravity(gravity);
        }

//        public Builder setList(int... ids) {
//            List<String> data = new ArrayList<>(ids.length);
//            for (int id : ids) {
//                data.add(getString(id));
//            }
//            return setList(data);
//        }
//
//        public Builder setList(String... data) {
//            return setList(Arrays.asList(data));
//        }

        @SuppressWarnings("all")
//        public Builder setList(List data) {
//            mAdapter.setData(data);
//            return this;
//        }

        public Builder setCancel(@StringRes int id) {
            return setCancel(getString(id));
        }

        public Builder setCancel(CharSequence text) {
            return this;
        }

        public Builder setAutoDismiss(boolean dismiss) {
            mAutoDismiss = dismiss;
            return this;
        }

        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        @Override
        public void onClick(View v) {
            if (mAutoDismiss) {
                dismiss();
            }

            if (v == mCancelView) {
                if (mListener != null) {
                    mListener.onCancel(getDialog());
                }
            }
        }
    }

    public interface OnListener<T> {

        /**
         * 选择条目时回调
         */
        void onSelected(BaseDialog dialog, int position, T t);

        /**
         * 点击取消时回调
         */
        default void onCancel(BaseDialog dialog) {
        }
    }
}